<template>
  <div :class="className" :style="{height:height,width:width}"/>
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import { debounce } from '@/utils'

export default {
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
      userAllCount: 0,
      userJoinCount: 0, //待入厂
      userEnsureCount: 0,//待确认
      userSplitCount: 0,//待分配
      userInCount: 0//待分配
  },
  data() {
    return {
      chart: null
    }
  },
    watch:{
        userAllCount(){
            this.initChart()
        }
    },
  mounted() {
    this.initChart()
    this.__resizeHandler = debounce(() => {
      if (this.chart) {
        this.chart.resize()
      }
    }, 100)
    window.addEventListener('resize', this.__resizeHandler)
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    window.removeEventListener('resize', this.__resizeHandler)
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c} ({d}%)'
        },
        calculable: true,
          legend: {
              left: 'center',
              bottom: '10',
              data: ['待分配', '待入职', '待确认', '在职']
          },
        series: [
          {
            name: '用户状态',
            type: 'pie',
            roseType: 'radius',
            radius: [15, 95],
            center: ['50%', '38%'],
            data: [
              { value: this.userSplitCount, name: '待分配' },
              { value: this.userJoinCount, name: '待入职' },
              { value: this.userEnsureCount, name: '待确认' },
              { value: this.userInCount, name: '在职' }
            ],
            animationEasing: 'cubicInOut',
            animationDuration: 2600
          }
        ]
      })
    }
  }
}
</script>
